<?php
namespace PHP_5UCMS\Controller;
use Think\Controller;
use Think\Storage;

class IndexController extends Controller {
	
	//如果没安装，则跳到安装界面
	protected function _initialize(){
		$lockpath = APP_PATH . 'Install/Data/install.lock';
		if(!Storage::has($lockpath)){
			redirect('./install.php');
		}
	} 
	
	//首页代码
    public function index(){
		$content = $this->fetch(); 
		$content = qss5ucms($content); //模板标签转换
		$content = ChageATurl($content,C('templatedir')); //修改资源文件路径
		$this->show($content);  
		if(Get_Spider()){echo C('indexcopyright');};//获取蜘蛛信息
    }
	
	//内容跳转
    public function redirectcontent(){  
		$id = $_GET['id'];
		if(is_numeric($id) and $id > 0){
			redirect(U('content/index','id='.$id));
		} else {
			$this -> error('内容参数不正确！',U('index'),3);	
		}
    }
	
	//栏目跳转
    public function redirectchannel(){  
		$id = $_GET['id'];
		if(is_numeric($id) and $id > 0){
			redirect(U('channel/index','id='.$id));
		} else {
			$this -> error('栏目参数不正确！',U('index'),3);	
		}
    }
	
	//自定义页跳转
    public function redirectdiypage(){  
		$id = $_GET['id'];
		if(is_numeric($id) and $id > 0){
			redirect(U('index/diypage','id='.$id));
		} else {
			$this -> error('自定义页参数不正确！',U('index'),3);	
		}
    }
	
	//自定义页功能
    public function diypage(){  
		$id = $_GET['id'];
		if(!is_numeric($id)){
			$this -> error('自定义页参数不正确！',U('index'),3);	
		}
		//读取自定义页面信息
		$qinfo = M('diypage') -> find($id);
		
		if(is_null($qinfo)){
			$this -> error('该自定义页不存在或已被删除！',U('index'),3);
		}	
		
		$tpl = $qinfo['tpl'];
	 	$tpl = str_ireplace('.html','',$tpl);
		//是否存在模板嵌套
		if(!is_null($tpl)){$tpl='common_default';} //默认只包含一个{tag:inside}的模板文件，不通过它 不能解析标签
		$content = $this->fetch($tpl);
		$content = ChageATurl($content,C('templatedir')); //修改资源文件路径 
		$content = str_ireplace('{field:id}',$id,$content);
		$content = str_ireplace('{field:diypageurl}',U('channel/diypage','id='.$id),$content);
		$content = str_ireplace('{field:dir}',$qinfo['Dir'],$content);
		$content = str_ireplace('{field:title}',$qinfo['Title'],$content);
		$content = str_ireplace('{field:keywords}',$qinfo['Keywords'],$content);
		$content = str_ireplace('{field:description}',$qinfo['Description'],$content);
		$content = str_ireplace('{tag:inside}',$qinfo['Html'],$content);
		$content = qss5ucms($content); //模板标签转换
		
		//
		
		//echo $tpl;
		//$this->show($content);   
		//$tpl = str_ireplace('_',':',$tpl);
		
		//$this -> buildHtml($id,'./','common_default'); 
		
		
		$a = $this -> show($content);
		
		file_put_contents('./1.html', '生成结果<hr>'.$a);
		
    } 
	
	

	 
	//测试功能2
	public function test2(){  
 
	 
    }	
	
	//测试功能3
	public function test3(){  
 		if(addwatermark('/uploadfile/201508/19/1439971458.jpg')){echo 1;}else{echo 0;}
    }
	
	//功能测试
    public function test(){ 
 
    }
	
	//空操作
	public function _empty(){
		error_404();
    }
}